<template>
<div class="bargain-active-index">
    <!--搜索表单-->
    <div class="d-b-c">
      <div class="mb18">
        <el-button size="small" icon="Plus" type="primary" @click="addClick"
          >{{$t('plus.bargain_add')}}</el-button
        >
      </div>
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label=""
          ><el-input
            v-model="searchForm.search"
            :placeholder="$t('plus.input_product_name')"
          ></el-input
        ></el-form-item>
        <el-form-item
          ><el-button type="primary" icon="Search" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          ></el-form-item
        >
      </el-form>
    </div>

    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="title" :label="$t('plus.activity_name')"></el-table-column>
          <el-table-column
            prop="startTime"
            :label="$t('plus.start_date')"
            width="150"
          ></el-table-column>
          <el-table-column
            prop="endTime"
            :label="$t('plus.end_time')"
            width="150"
          ></el-table-column>
          <el-table-column
            prop="statusText"
            :label="$t('plus.activity_status')"
            width="120"
          ></el-table-column>
          <el-table-column
            prop="productNum"
            :label="$t('plus.product_count')"
            width="70"
          ></el-table-column>
          <el-table-column
            prop="totalSales"
            :label="$t('plus.order_count')"
            width="70"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('plus.created_at')"
            width="150"
          ></el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="90">
            <template #default="scope">
              <el-button
                @click="editsClick(scope.row.bargainActivityId)"
                type="text"
                size="small"
                >{{$t('plus.edit')}}</el-button
              >
              <el-button
                @click="deleteClick(scope.row.bargainActivityId)"
                type="text"
                size="small"
                >{{$t('plus.delete')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import BargainApi from "@/api/bargain.js";
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*搜索表单对象*/
      searchForm: {},
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      self.loading = true;
      BargainApi.bargainList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.records;
        self.totalDataNumber = res.data.total;
      }).catch(error => {});
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      this.curPage = val;
      this.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*添加砍价活动*/
    addClick() {
      this.$router.push("/plus/bargain/active/add");
    },
    /*修改砍价活动*/
    editsClick(e) {
      let self = this;
      this.$router.push({
        path: "/plus/bargain/active/edit",
        query: {
          bargain_activity_id: e
        }
      });
    },
    /*删除砍价活动*/
    deleteClick(e) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        BargainApi.deleteBargain({
          bargainActivityId: e
        }, true).then(data => {
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getData();
        }).catch(error => {});
      }).catch(() => {
        ElMessage({
          type: "info",
          message: $t("plus.cancel_delete")
        });
      });
    }
  }
};
</script>

